import { defineStore } from 'pinia'
import { ref, computed } from 'vue'

/**
 * defineStore
 * 参数1：给仓库取个名字
 * 参数2：箭头函数，里面写仓库的逻辑
 * 参数3：仓库持久化设置
 */
export const useCounterStore = defineStore('my-counter', () => {
  // vuex(state、getter、mutations、action)

  // state
  const count = ref(100)

  // getter
  const doubleCount = computed(() => count.value * 2)

  // mutations
  const add = (payload: number) => {
    count.value += payload
  }

  // action
  const asyncAdd = (payload: number) => {
    setTimeout(() => {
      count.value += payload
    }, 1000)
  }

  // 这个必须要有（它没有语法糖的，想想setup）
  return {
    count,
    doubleCount,
    add,
    asyncAdd
  }
})
